
/*
 * @Description: 用户登录日志
 * @Author: DSY
 * @Date: 2022-11-25 16:15:09
 * @LastEditors: DSY
 * @LastEditTime: 2022-11-25 16:15:09
 */

import { defineComponent, computed, reactive, ref } from 'vue'
import { log } from '/@/utils/log'
import { ElMessage } from 'element-plus'
import { cloneDeep, assign } from 'lodash-es'
import { OnlineModel } from '/@/api/system/model/onlineModel'
import { apiOnlineSave, apiOnlineUpdateById } from '/@/api/system/online'

export default defineComponent({
  name: 'OnlineForm',
  emits: ['refreshTableData'],
  setup(props, { emit, expose }) {
    //////////////////////////////////////////////////
    // 属性
    //////////////////////////////////////////////////
    const dbFormRef = ref()
    const loading = ref(true)

    /**
     * 表单操作标记
     */
    const action = ref('add')

    /**
     * 显示弹框
     */
    const visible = ref(false)

    /**
     * 标题
     */
    const title = computed(() => {
      let rtn = ''
      switch (action.value) {
        case 'add':
          rtn = '新增'
          break
        case 'edit':
          rtn = '编辑'
          break
        default:
          rtn = '查看'
          break
      }
      return rtn
    })

    /**
     * 公共
     */
    const state = reactive({ })

    //////////////////////////////////////////////////
    // 表单
    //////////////////////////////////////////////////

    const defaultForm = {
      id: '',  // 日志编号
      ip: '',  // IP地址
      loginName: '',  // 登录名
      type: '',  // 类别
      loginType: ''  // 用户登录类型
    }

    let dbForm = reactive<OnlineModel>(cloneDeep(defaultForm))

    //////////////////////////////////////////////////
    // 表单验证规则
    //////////////////////////////////////////////////

    /**
     * 表单验证规则
     */
    const rules = reactive({
      ip: [
        {
          required: true,
          message: '请输入IP地址',
          trigger: 'blur'
        },  {
          max: 100,
          message: '最多可输入100个字符',
          trigger: 'blur'
        }
      ],
      loginName: [
        {
          required: true,
          message: '请输入登录名',
          trigger: 'blur'
        },  {
          max: 100,
          message: '最多可输入100个字符',
          trigger: 'blur'
        }
      ],
      type: [
        {
          required: true,
          message: '请输入类别',
          trigger: 'blur'
        },  {
          max: 10,
          message: '最多可输入10个字符',
          trigger: 'blur'
        }
      ],
      loginType: [
        {
          required: true,
          message: '请输入用户登录类型',
          trigger: 'blur'
        },  {
          max: 10,
          message: '最多可输入10个字符',
          trigger: 'blur'
        }
      ]
    })

    //////////////////////////////////////////////////
    // 函数
    //////////////////////////////////////////////////

    /**
     * 打开弹框
     */
    function handleOpen(actionParam: string, rowParam?: OnlineModel) {
      visible.value = true
      loading.value = false
      action.value = actionParam
      if (['edit', 'view'].indexOf(actionParam) > -1 && rowParam) {
        assign(dbForm, cloneDeep(rowParam))
      }
    }

    /**
     * 关闭弹框
     */
    function handleClose() {
      assign(dbForm, cloneDeep(defaultForm))
      visible.value = false
    }

    /**
     * 提交表单
     */
    function handleSubmit(isContinue: boolean) {
      dbFormRef.value.validate((valid: boolean) => {
        if (valid) {
          if (action.value === 'add') {
            addFun(isContinue)
          } else {
            editFun()
          }
        } else {
          log.warn('用户登录日志', '表单校验失败')
        }
      })
    }

    /**
     * 新增
     * @param isContinue 是否新增并继续
     */
    function addFun(isContinue: boolean) {
      loading.value = true
      apiOnlineSave(dbForm)
        .then((result) => {
          if (result?.success) {
            handleReset(!isContinue)
            ElMessage.success(result.message)
          } else {
            ElMessage.error(result?.message)
          }
        })
        .catch(() => {})
        .finally(() => {
          loading.value = false
        })
    }

    /**
     * 编辑
     * @returns
     */
    function editFun() {
      loading.value = true
      apiOnlineUpdateById(dbForm)
        .then((result) => {
          if (result?.success) {
            handleReset(true)
            ElMessage.success(result.message)
          } else {
            ElMessage.error(result?.message)
          }
        })
        .catch(() => {})
        .finally(() => {
          loading.value = false
        })
    }

    /**
     * 重置表单
     * @param isClosed 是否关闭弹框
     */
    function handleReset(isClosed: boolean) {
      assign(dbForm, cloneDeep(defaultForm))
      if (isClosed) {
        emit('refreshTableData')
        visible.value = false
      }
    }

    /**
     * 暴露函数
     */
    expose({ handleOpen })

    return {
      loading,
      visible,
      action,
      title,
      state,
      dbFormRef,
      dbForm,
      rules,
      handleOpen,
      handleClose,
      handleSubmit,
      handleReset
    }
  }
})
